<?php

$root = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
if (file_exists($root.'/wp-load.php')) {
    // WP 2.6
    require_once($root.'/wp-load.php');
} else {
    // Before 2.6
    require_once($root.'/wp-config.php');
}

include_once(WHATSIT_PATH.'lib/database.interface.php');

class Wordpress_Database implements Whatsit_Database
{
    private $username;
    private $options;
    private $prefs;
    private $prefs_changed;
    private $opts_changed;

    function __construct($username)
    {
        $this->username = $username;

        $this->opts_changed = false;
        $this->prefs_changed = false;

        $this->options = get_option('widget_whatsits');
        if (!is_array($this->options)) {
            $this->options = array();
            $this->opts_changed = true;
        }

        $this->prefs = get_option('widget_whatsits_prefs');
        if (!is_array($this->prefs)) {
            $this->prefs = array();
            $this->opts_changed = true;
        }
    }

    function __destruct()
    {
        if ($this->opts_changed) {
            update_option('widget_whatsits', $this->options);
        }
        if ($this->prefs_changed) {
            update_option('widget_whatsits_prefs', $this->prefs);
        }
    }

    public function getWhatsits($pane_name)
    {
        return array();
    }

    public function setPref($id, $pref_name, $pref_value)
    {
        if (!isset($this->prefs[$id]) || !is_array($this->prefs[$id])) {
            $this->prefs[$id] = array();
        }
        if (!isset($this->prefs[$id][$pref_name]) || $this->prefs[$id][$pref_name] != $prev_value) {
            $this->prefs_changed = true;
        }
        $this->prefs[$id][$pref_name] = $pref_value;
    }

    public function addWhatsit($url, $pane)
    {
        throw new Exception("This is done through the wordpress widget administration pane");
        return;
    }

    public function moveWhatsit($whatsit_id, $from_pane, $to_pane, $after_whatsit_id)
    {
        throw new Exception("This is done through the wordpress widget administration pane");
        return;
    }

    private function getPrefs($id)
    {
        if (!isset($this->prefs[$id]) || !is_array($this->prefs[$id])) {
            $this->prefs[$id] = array();
            $this->prefs_changed = true;
        }
        return $this->prefs[$id];
    }

    public function getWhatsit($id)
    {
        $whatsit = new Whatsit($id, $this->options[$id]['url']);

        foreach ($this->getPrefs($id) as $pref_key => $pref_value) {
            $whatsit->setPref($pref_key, $pref_value);
        }
        if ($this->options[$id]['title'] != null) {
            $whatsit->setTitle($this->options[$id]['title']);
        }
        if ($this->options[$id]['height'] != null) {
            $whatsit->setTitle((int)$this->options[$id]['height']);
        }
        return $whatsit;
    }

    public function getOption($id, $option_name)
    {
        if (!isset($this->prefs[$id]) || !is_array($this->prefs[$id])) {
            $this->options[$id] = array('url'=>null, 'title'=>null, 'height'=>null);
            $this->opts_changed = true;
        }
        return $this->options[$id][$option_name];
    }

    public function setOption($id, $option_name, $option_value)
    {
        if (!isset($this->options[$id]) || !is_array($this->options[$id])) {
            $this->options[$id] = array('url'=>null, 'title'=>null, 'height'=>null);
            $this->opts_changed;
        }
        $this->options[$id][$option_name] = $option_value;
    }
}